package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    //查询
    @Override
    public Page selectStudent( Integer  currentPage,Integer pageSize) {
        InputStream is =null;
        SqlSession sqlSession = null;
        Page page = null;
        try {
             is = Resources.getResourceAsStream("MyBatisConfig.xml");
             sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
             StudentMapper studentMapperSelect= sqlSession.getMapper(StudentMapper.class);
             page = PageHelper.startPage(currentPage, pageSize);
             studentMapperSelect.selectAll();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return page;
    }
    //添加
    @Override
    public int addStu(Student student) {
        InputStream is =null;
        SqlSession sqlSession =null;
        int add =0;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
            StudentMapper studentMapperAdd = sqlSession.getMapper(StudentMapper.class);
            add= studentMapperAdd.addStu(student);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return add;
    }
    //修改
    @Override
    public int updateStu(Student student) {
        InputStream is =null;
        SqlSession sqlSession =null;
        int i=0;
        try {
             is = Resources.getResourceAsStream("MyBatisConfig.xml");
             sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
             StudentMapper studentMapperUpdate = sqlSession.getMapper(StudentMapper.class);
             i = studentMapperUpdate.updaptStu(student);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return i;
    }

    @Override
    public int deleteStu(String number) {
        InputStream is =null;
        SqlSession sqlSession =null;
        int i=0;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            sqlSession = new SqlSessionFactoryBuilder().build(is).openSession(true);
            StudentMapper studentMapperUpdate = sqlSession.getMapper(StudentMapper.class);
            i = studentMapperUpdate.deleteStu(number);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(i);
        return i;
    }
}
